package com.neusoft.hifly.lock.strategy.impl;

import org.apache.commons.lang3.StringUtils;
import org.redisson.config.Config;

import com.neusoft.hifly.lock.config.RedissonConfig;
import com.neusoft.hifly.lock.constant.GlobalConstant;
import com.neusoft.hifly.lock.strategy.RedissonConfigService;
import com.neusoft.hifly.lock.utils.Log;

/**
 * 单机部署Redisson配置
 */
public class StandaloneConfigImpl implements RedissonConfigService {
	/**
	 * 根据不同的Redis配置策略创建对应的Config
	 *
	 * @param redissonProperties
	 *            redisson配置
	 * @return Config
	 */
	@Override
	public Config createRedissonConfig(final RedissonConfig redissonProperties) {
		final Config config = new Config();
		try {
			final String address = redissonProperties.getAddress();
			final String password = redissonProperties.getPassword();
			final int database = redissonProperties.getDatabase();
			final String redisAddr = GlobalConstant.REDIS_CONNECTION_PREFIX.getConstantValue() + address;
			config.useSingleServer().setAddress(redisAddr);
			config.useSingleServer().setDatabase(database);
			//密码可以为空
			if (StringUtils.isNotBlank(password)) {
				config.useSingleServer().setPassword(password);
			}
			Log.info("初始化[单机部署]方式Config,redisAddress:" + address);
		} catch (final Exception e) {
			Log.info("单机部署 Redisson init error", e);
		}
		return config;
	}
}
